package zabbix

import (
	"encoding/json"
	"errors"
)

func ValuemapCreateData() (string,map[string]interface{}) {
	method := "valuemap.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Service state",
        "mappings": [
            {
                "value": "0",
                "newvalue": "Down"
            },
            {
                "value": "1",
                "newvalue": "Up"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ValuemapDeleteData() (string,map[string]interface{}) {
	method := "valuemap.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "1",
        "2"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ValuemapGetData() (string,map[string]interface{}) {
	method := "valuemap.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ValuemapUpdateData() (string,map[string]interface{}) {
	method := "valuemap.Update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "valuemapid": "2",
        "name": "Device status"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}



func ValuemapCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ValuemapCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ValuemapDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ValuemapDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ValuemapGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ValuemapGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ValuemapUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ValuemapUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


